package alexAI;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;

import legacy.Log;



public class MoveManager {

	private Log m_history;
	private Log m_tree;
	private String data;
	private static final MoveManager INSTANCE = new MoveManager();
	
	
	
	private MoveManager(){
		
		}
	
	 public static MoveManager getInstance() {
	        return INSTANCE;
	    }

	public void add(int[][] Nouveau_mouvement){
		m_history.addFirst(Nouveau_mouvement);
	}
	public String getTree(ArrayList<int[][]> valid_moves, int[][] next_move){
		int[][] leMove = new int[1][1];
		//get all possible moves for this level
		for (int i = 0 ; i < valid_moves.size() ; i++)
		   {
		    leMove = valid_moves.get(i);
		    data += ("(" + leMove[0][0] + ";" + leMove[0][1] + ") -> (" + leMove[1][0] + ";" + leMove[1][1] + ")  -- Score: " + leMove[2][0]) + "/n";
		   }
		// get move that was made at this level   
		data += ("(" + next_move[0][0] + ";" + next_move[0][1] + ") -> (" + next_move[1][0] + ";" + next_move[1][1] + ")") + "/n";
		m_history.addFirst(next_move);
		
		return this.data;
	}
	public void getExploredTree(){
		
	}
	public void undoLast(){
		if(m_history.size() > 0){
			int[][] undo;
			undo = m_history.removeLast();
			//undo.undo();
			m_history.removeLast();
		}
		
	}
	
	public void createHistory() {
		String fileName = "LogBranchTree";
		fileName += "_" + Utils.getTimeStamp();
		try {
			// Create file 
			FileWriter fstream = new FileWriter(fileName + ".debug",true);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write("\r\n");
			out.write(data);
			/*for(int i = 0; i < m_history.size(); i++) {
				out.write(m_history.removeLast().toString() + "\r\n");
			}*/
			//Close the output stream
			out.close();			
		}catch (Exception e){//Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}
}
